package edu.taru.view;

import javax.swing.JFrame;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import edu.taru.pojo.User;
import edu.taru.service.*;

import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JComboBox;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Choice;
import java.awt.Color;

/**
 * 注册界面
 * @author chen
 *
 */
public class RegisterView extends JFrame{
	private JTextField textField;
	private JTextField textField_1;
	private JPasswordField passwordField;
	private JPasswordField passwordField_1;
	private JTextField textField_2;
	String citys[] = {"北京","广州","上海","深圳","杭州","长沙","乌鲁木齐","兰州","西宁","郑州","武汉","成都","贵阳","天津"};
	String years[] = {"1987","1988","1989","1990","1991","1992","1993","1994","1995","1996","1997","1998","1999"};
	String months[] = {"1","2","3","4","5","6","7","8","9","10","11","12"};
	String dates[] = {"1","2","3","4","5","6","7","8","9","10",
			"11","12","13","14","15","16","17","18","19","20",
			"21","22","23","24","25","26","27","28","29","30","31"};
	private JTextField textField_3;
	public RegisterView() {
		getContentPane().setForeground(new Color(102, 204, 153));
		setSize(599,442);
		setVisible(true);
		setTitle("注册用户");
		setLocationRelativeTo(null);
		getContentPane().setLayout(null);
		
		JLabel label = new JLabel("用户名");
		label.setBounds(138, 61, 54, 15);
		getContentPane().add(label);
		
		textField = new JTextField();
		textField.setBounds(197, 58, 143, 21);
		getContentPane().add(textField);
		textField.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("姓名");
		lblNewLabel.setBounds(138, 92, 54, 15);
		getContentPane().add(lblNewLabel);
		
		textField_1 = new JTextField();
		textField_1.setBounds(197, 89, 143, 21);
		getContentPane().add(textField_1);
		textField_1.setColumns(10);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(197, 121, 143, 21);
		getContentPane().add(passwordField);
		
		JLabel label_1 = new JLabel("密码");
		label_1.setBounds(138, 124, 54, 15);
		getContentPane().add(label_1);
		
		JLabel label_2 = new JLabel("确认密码");
		label_2.setBounds(138, 155, 54, 15);
		getContentPane().add(label_2);
		
		passwordField_1 = new JPasswordField();
		passwordField_1.setBounds(197, 152, 143, 21);
		getContentPane().add(passwordField_1);
		
		textField_2 = new JTextField();
		textField_2.setBounds(197, 183, 143, 21);
		getContentPane().add(textField_2);
		textField_2.setColumns(10);
		
		JLabel label_3 = new JLabel("邮箱");
		label_3.setBounds(138, 186, 54, 15);
		getContentPane().add(label_3);
		
		JLabel lblNewLabel_1 = new JLabel("性别");
		lblNewLabel_1.setBounds(138, 211, 54, 15);
		getContentPane().add(lblNewLabel_1);
		
		JRadioButton radioButton = new JRadioButton("男");
		radioButton.setBounds(197, 210, 48, 23);
		getContentPane().add(radioButton);
		
		JRadioButton rdbtnNewRadioButton = new JRadioButton("女");
		rdbtnNewRadioButton.setBounds(246, 210, 121, 23);
		getContentPane().add(rdbtnNewRadioButton);
		
		ButtonGroup  bg =new ButtonGroup();
		bg.add(radioButton);
		bg.add(rdbtnNewRadioButton);
		
		JLabel label_4 = new JLabel("城市");
		label_4.setBounds(138, 242, 54, 15);
		getContentPane().add(label_4);
		
		JComboBox comboBox = new JComboBox(citys);
		comboBox.setBounds(197, 239, 81, 21);
		getContentPane().add(comboBox);
		
		JLabel label_5 = new JLabel("出生日期");
		label_5.setBounds(138, 273, 54, 15);
		getContentPane().add(label_5);
		
		JComboBox comboBox_1 = new JComboBox(years);
		comboBox_1.setBounds(197, 270, 81, 21);
		getContentPane().add(comboBox_1);
		
		JComboBox comboBox_2 = new JComboBox(months);
		comboBox_2.setBounds(308, 270, 59, 21);
		getContentPane().add(comboBox_2);
		
		JComboBox comboBox_3 = new JComboBox(dates);
		comboBox_3.setBounds(390, 270, 59, 21);
		getContentPane().add(comboBox_3);
		
		JLabel label_6 = new JLabel("年");
		label_6.setBounds(286, 273, 54, 15);
		getContentPane().add(label_6);
		
		JLabel label_7 = new JLabel("月");
		label_7.setBounds(369, 273, 54, 15);
		getContentPane().add(label_7);
		
		JLabel label_8 = new JLabel("日");
		label_8.setBounds(459, 273, 54, 15);
		getContentPane().add(label_8);
		
		JButton button = new JButton("注册");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//获取参数
				String username = textField.getText();//用户名
				String name=textField_1.getText();//姓名
				String password=new String(passwordField.getPassword());//密码
				String user_email= textField_2.getText();//邮箱
				String sex="男";//性别
				if(radioButton.isSelected()) {
					sex="女";
				}
				int index = comboBox.getSelectedIndex();//城市
				String city=citys[index];
				int yearIdext = comboBox_1.getSelectedIndex();//年
				String year = years[yearIdext];
				int monthIndex = comboBox_2.getSelectedIndex();//月
				String month = months[monthIndex];
				int dateIndex = comboBox_3.getSelectedIndex();//日
				String date = dates[dateIndex];
				String birthday= year+"-"+month+"-"+date;
				String telphone = textField_3.getText();
				User user = new User();
				user.setUsername(username);
				user.setPassword(password);
				user.setCity(city);
				user.setBirthday(birthday);
				user.setEmail(user_email);
				user.setSex(sex);
				user.setName(name);
				user.setTelphone(telphone);
				Services services = new Services();
				services.register(user);
				if(user==null) {
					JOptionPane.showMessageDialog(RegisterView.this, "注册失败");
				}else {
					JOptionPane.showMessageDialog(RegisterView.this, "注册成功");
				}
				
				RegisterView.this.dispose();
				
				
				
			}
		});
		button.setBounds(224, 348, 93, 23);
		getContentPane().add(button);
		
		JLabel label_9 = new JLabel("用户注册");
		label_9.setBounds(224, 26, 54, 15);
		getContentPane().add(label_9);
		
		JLabel label_10 = new JLabel("电话号码");
		label_10.setBounds(138, 304, 54, 15);
		getContentPane().add(label_10);
		
		textField_3 = new JTextField();
		textField_3.setBounds(197, 301, 143, 21);
		getContentPane().add(textField_3);
		textField_3.setColumns(10);
		
		
	}
}
